package org.xa3apm.vote.hibernate.entities;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.Collection;

import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.Id;
import javax.persistence.ManyToOne;
import javax.persistence.NamedQueries;
import javax.persistence.NamedQuery;
import javax.persistence.OneToMany;
import javax.persistence.Table;

@Entity
@Table(name = "tMunicipality")
@NamedQueries({
		@NamedQuery(name = "Municipality.getById", query = "SELECT M FROM Municipality M WHERE M.id = :id"),
		@NamedQuery(name = "Municipality.getAll", query = "SELECT M FROM Municipality M"),
		// @NamedQuery(name="getMunByProvId",query="SELECT M FROM Province P LEFT JOIN P.municipalities M")
		@NamedQuery(name = "Municipality.getAllByProvinceId", query = "SELECT M FROM Municipality M  WHERE M.province = :province ")
// @NamedQuery(name="Municipality.getByName",query="SELECT M FROM Municipality M  WHERE M.name = :name ")
})
public class Municipality implements Serializable {
	/**
	 * 
	 */
	private static final long serialVersionUID = -8117086267522826368L;
	@Id
	@GeneratedValue
	@Column(name = "id", nullable = false, unique = true)
	private Long id;
	@Column(name = "name")
	private String name;
	@ManyToOne(targetEntity = Province.class, optional = false)
	private Province province;
	@OneToMany(mappedBy = "municipality", orphanRemoval = true)
	private Collection<Settlement> settlement;

	public Municipality() {
		// TODO Auto-generated constructor stub
	}

	/**
	 * @param id
	 * @param municipalityName
	 * @param province
	 * @param settlement
	 */
	public Municipality(Long id, String mName, Province province,
			Collection<Settlement> settlement) {
		this.id = id;
		this.name = mName;
		this.province = province;
		this.settlement = settlement;
	}

	/**
	 * @return the id
	 */
	public final Long getId() {
		return id;
	}

	/**
	 * @param id
	 *            the id to set
	 */
	public final void setId(Long mId) {
		this.id = mId;
	}

	/**
	 * @return the municipalityName
	 */
	public final String getName() {
		return name;
	}

	/**
	 * @param municipalityName
	 *            the municipalityName to set
	 */
	public final void setName(String mName) {
		this.name = mName;
	}

	/**
	 * @return the province
	 */
	public final Province getProvince() {
		return province;
	}

	/**
	 * @param province
	 *            the province to set
	 */
	public final void setProvince(Province province) {
		this.province = province;
	}

	public Collection<Settlement> getSettlement() {
		if (settlement == null) {
			settlement = new ArrayList<Settlement>();
		}
		return settlement;
	}

	public void setSettlement(Collection<Settlement> settlement) {
		this.settlement = settlement;
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see java.lang.Object#hashCode()
	 */
	@Override
	public int hashCode() {
		final int prime = 31;
		int result = 1;
		result = prime * result + ((id == null) ? 0 : id.hashCode());
		result = prime * result + ((name == null) ? 0 : name.hashCode());
		result = prime * result
				+ ((province == null) ? 0 : province.getId().hashCode());
		result = prime * result
				+ ((settlement == null) ? 0 : settlement.hashCode());
		return result;
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see java.lang.Object#equals(java.lang.Object)
	 */
	@Override
	public boolean equals(Object obj) {
		if (this == obj)
			return true;
		if (obj == null)
			return false;
		if (!(obj instanceof Municipality))
			return false;
		Municipality other = (Municipality) obj;
		if (id == null) {
			if (other.id != null)
				return false;
		} else if (!id.equals(other.id))
			return false;
		if (name == null) {
			if (other.name != null)
				return false;
		} else if (!name.equals(other.name))
			return false;
		if (province == null) {
			if (other.province != null)
				return false;
		} else if (!province.getId().equals(other.province.getId()))
			return false;
		if (settlement == null) {
			if (other.settlement != null)
				return false;
		} else if (!settlement.equals(other.settlement))
			return false;
		return true;
	}

	@Override
	public String toString() {
		// return just id and name
		return id + ", " + name + " [provinceId: " + province.getName() + "]";
	}
}
